package cn.dlbj.util;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStreamReader;
import java.sql.Connection;
import java.sql.DriverManager;
import java.util.Random;
import java.util.ResourceBundle;

public class DBUtil {
	
	static String jdbcDriver;
	static String jdbcURL;
	static String dbUser;
	static String dbPassword;
	
	static {     	
        ResourceBundle rb = ResourceBundle.getBundle("config");
		jdbcDriver = rb.getString("driverClassName");
		jdbcURL = rb.getString("jdbc_url");
		dbUser = rb.getString("jdbc_username");
		dbPassword = rb.getString("jdbc_password");
	}
	
	public static Connection getConnection() throws Exception {
		Class.forName(jdbcDriver);
		Connection con = DriverManager.getConnection(jdbcURL, dbUser, dbPassword);
		return con;
	}
	
	public static String genInviteCode(int id) {
		String idStr;
		if(id < 10000) {
			id += 10000;
			idStr = String.valueOf(id).substring(1);
		} else {
			idStr = String.valueOf(id);
		}
		String result = "";
		Random random = new Random();
		for(int i = 0; i < idStr.length(); i++) {
			result = result + idStr.substring(i, i + 1) + random.nextInt(10);
		}
		return result;
	}
	
	public static void main(String[] args) {
		try {
            String encoding="UTF-8";
            File file = new File("c://Book1.csv");
            if(file.isFile() && file.exists()) { //判断文件是否存在
                InputStreamReader read = new InputStreamReader(new FileInputStream(file), encoding);//考虑到编码格式
                BufferedReader bufferedReader = new BufferedReader(read);
                String lineTxt = null;
                while((lineTxt = bufferedReader.readLine()) != null) {
                    //System.out.println(lineTxt);
                	String[] fields = lineTxt.split(",");
                	String id = java.util.UUID.randomUUID().toString();
                	String xm = fields.length > 0 ? fields[0].trim() : "";
                	String sjh = fields.length > 1 ? fields[1].trim() : "";
                	if(sjh.length() > 11) {
                		sjh = sjh.substring(0,11);
                	}
                	String email = fields.length > 2 ? fields[2].trim() : "";
                	String ssdz = fields.length > 3 ? fields[3].trim() : "";
                	String xkzy = fields.length > 4 ? fields[4].trim() : "";
                	//System.out.println(fields.length);
                	System.out.println(
                			"insert into z_zyz (id,xm,sjh,email,ssdz,xkzy,password,bool,tjr) values('" + id + "','" 
                	         + xm + "','" + sjh + "','" + email + "','" 
                			 + ssdz + "','" + xkzy + "','666666"  + "','N','" + "陈志勇" + "');");
                }
                read.close();
            }else{
            	System.out.println("找不到指定的文件");
            }
		} catch (Exception e) {
			System.out.println("读取文件内容出错");
			e.printStackTrace();
		}
	}

}
